package com.geeklan.base.utils;

import android.util.Log;

public class MLog {
    private static final String TAG = "MLM";
    private static final boolean DEFAULT_DEBUG_ENABLED = true;
    private static boolean isDebugEnabled = DEFAULT_DEBUG_ENABLED; // 是否启用调试日志

    // 设置日志输出开关
    public static void setDebugEnabled(boolean enabled) {
        isDebugEnabled = enabled;
    }

    public static void d(String message) {
        if (isDebugEnabled) {
            Log.d(getTag(null), message);
        }
    }

    public static void d(String tag, String message) {
        if (isDebugEnabled) {
            Log.d(getTag(tag), message);
        }
    }

    public static void i(String message) {
        if (isDebugEnabled) {
            Log.i(getTag(null), message);
        }
    }

    public static void i(String tag, String message) {
        if (isDebugEnabled) {
            Log.i(getTag(tag), message);
        }
    }

    public static void w(String message) {
        if (isDebugEnabled) {
            Log.w(getTag(null), message);
        }
    }

    public static void w(String tag, String message) {
        if (isDebugEnabled) {
            Log.w(getTag(tag), message);
        }
    }

    public static void e(String message) {
        if (isDebugEnabled) {
            Log.e(getTag(null), message);
        }
    }

    public static void e(String tag, String message) {
        if (isDebugEnabled) {
            Log.e(getTag(tag), message);
        }
    }

    // 获取标签，如果传入的标签为空，则使用默认标签
    private static String getTag(String tag) {
        return tag != null ? tag : getCallerClassName();
    }

    // 获取调用者类名
    private static String getCallerClassName() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (int i = 3; i < stackTrace.length; i++) {
            StackTraceElement element = stackTrace[i];
            if (!element.getClassName().equals(MLog.class.getName())) {
                String fullClassName = element.getClassName();
                return fullClassName.substring(fullClassName.lastIndexOf('.') + 1); // 返回简单类名
            }
        }
        return "Unknown";
    }
}

